package com.pocketsweet.ui.uilib.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.pocketsweet.MLContext;
import com.pocketsweet.R;
import com.pocketsweet.model.MLArticleComment;
import com.pocketsweet.model.MLUser;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.Profile;
import com.pocketsweet.utils.StringUtils;
import com.pocketsweet.utils.ToolKits;

/**
 * 
 * @author Kam
 * 
 */
public class ArticleCommentListAdapter extends
		BaseListAdapter<MLArticleComment> {

	public ArticleCommentListAdapter(Context ctx) {
		super(ctx);
	}

	public ArticleCommentListAdapter(Context ctx, List<MLArticleComment> datas) {
		super(ctx, datas);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_article_comment,
					parent, false);
			viewHolder = new ViewHolder(convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.refreshView(datas.get(position), position);
		return convertView;
	}

	private class ViewHolder implements View.OnClickListener {
		ImageView imgAvatar;
		TextView tvuserName;
		TextView tvFrom;
		TextView tvUserComment;
		TextView tvCommentTime;
		MLArticleComment item;
		private View parent;

		private ViewHolder(View parent) {
			this.parent = parent;
			initView(parent);
		}

		public void initView(View parent) {
			imgAvatar = (ImageView) parent.findViewById(R.id.imgAvatarComment);
			tvuserName = (TextView) parent.findViewById(R.id.tvuserName);
			tvFrom = (TextView) parent.findViewById(R.id.tvFrom);
			tvUserComment = (TextView) parent.findViewById(R.id.tvUserComment);
			tvCommentTime = (TextView) parent.findViewById(R.id.tvCommentTime);
			imgAvatar.setOnClickListener(this);
		}

		public void refreshView(MLArticleComment item, int i) {
			this.item = item;
			MLUser mlUser = new MLUser();
			mlUser = item.getUser();
			if (mlUser == null)
				return;
			String location = null;
			String avatar = mlUser.getAvatarUrl();
			if (avatar != null && !avatar.equals("")) {
				UserService.displaySquareRecommend(avatar, imgAvatar);
			} else {
				imgAvatar.setImageResource(R.drawable.default_user_avatar);
			}
			// 设置姓名
			tvuserName.setText(mlUser.getNickname());
			// 设置年龄
			if (!StringUtils.isEmpty(String.valueOf(mlUser.getAgeNum()))) {
			}
			// 设置性别颜色
			if (mlUser.getGender() == 1) {
				tvuserName.setTextColor(ctx.getResources().getColor(R.color.male_color));
			} else if (mlUser.getGender() == 2) {
				tvuserName.setTextColor(ctx.getResources().getColor(R.color.main_text_color_pink));
				// d.setColor(ctx.getResources().getColor(R.color.main_text_color));//
				// 设置颜色
				
			}

			// 设置地理位置
			location = mlUser.getLocationCity();
			if (StringUtils.isEmpty(location)) {
				location = mlUser.getLocationProvince();
			}
			tvFrom.setText(location);
			if (item.getReplyUser() != null) {
				tvUserComment.setText("回复" + item.getReplyUser().getNickname()
						+ ":  " + item.getContent());
			} else {
				tvUserComment.setText(item.getContent());
			}
			tvCommentTime.setText(ToolKits.getTimeDiff(item.getCreatedAt()));
//			tvCommentTime.setText(ToolKits.millisecs2DateString(mlUser
//					.getCreatedAt().getTime())
//					+ ","
//					+ ToolKits.showCommentTime(mlUser.getCreatedAt()));

		}

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.imgAvatarComment:
				if (item.getUser()!=null) {
					String selectedUserId = item.getUser().getObjectId();
					boolean isLiked = MLContext.getUserLikeID(selectedUserId) == 0 ? false
							: true;
					Intent intent = new Intent(ctx, Profile.class);
					intent.putExtra("userId", selectedUserId);
					intent.putExtra("isLiked", isLiked);
					ctx.startActivity(intent);
				}
				break;
			}
		}
	}
}
